package cn.ctvonline.android.kaopu.support;

import android.content.Context;

import com.amap.api.cloud.model.AMapCloudException;
import com.amap.api.cloud.model.LatLonPoint;
import com.amap.api.cloud.search.CloudSearch;

/**
 * Created by dujian on 15/11/26.
 */
public class AmapCloudSearcher {
    private boolean inited = false;
    private static AmapCloudSearcher instance = new AmapCloudSearcher();
    private CloudSearch mCloudSearch;
    private String mTableID;
    private double mLatitude;
    private double mLongitude;

    private AmapCloudSearcher() {
    }

    public static AmapCloudSearcher getInstance() {
        return instance;
    }

    public void init(Context context, String mTableID, double mLatitude, double mLongitude) {
        if (mCloudSearch == null) {
            mCloudSearch = new CloudSearch(context);//初始化查询类
            inited = true;
        }
        if (mTableID != null && !"".equals(mTableID)) {
            this.mTableID = mTableID;
            this.mLatitude = mLatitude;
            this.mLongitude = mLongitude;
        }
    }

    public void search(CloudSearch.OnCloudSearchListener onCloudSearchListener, String keyword, int scope) throws AMapCloudException {
        if (!inited) {
            throw new RuntimeException("AmapCloudSearcher not inited ... ");
        }
        mCloudSearch.setOnCloudSearchListener(onCloudSearchListener);//设置回调函数
        LatLonPoint centerPoint = new LatLonPoint(mLatitude, mLongitude);//设置中心点
        CloudSearch.SearchBound bound = new CloudSearch.SearchBound(centerPoint, scope);//设置检索范围
        CloudSearch.Query mQuery = new CloudSearch.Query(mTableID, keyword, bound);//设置查询条件
        mCloudSearch.searchCloudAsyn(mQuery);
    }

    public boolean isInited() {
        return inited;
    }

    public void destory() {
        instance = null;
    }
}
